﻿//Please add references
using System;
using System.Collections;//Please add references
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Text;
using EIM_Accounting.Common.DBUtility;

namespace Accounting.DAL
{
    /// <summary>
    /// 数据访问类:Holiday
    /// </summary>
    public partial class Holiday
    {
        public Holiday()
        { }

        #region Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Holiday");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Accounting.Model.Holiday model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Holiday(");
            strSql.Append("StaffID,Holiday)");
            strSql.Append(" values (");
            strSql.Append("@StaffID,@Holiday)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@StaffID", DbType.Int32,8),
                    new SQLiteParameter("@Holiday", DbType.String)};
            parameters[0].Value = model.StaffID;
            parameters[1].Value = model.HolidayDate;

            object obj = DbHelperSQLite.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Accounting.Model.Holiday model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Holiday set ");
            strSql.Append("StaffID=@StaffID,");
            strSql.Append("Holiday=@Holiday");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@StaffID", DbType.Int32,8),
                    new SQLiteParameter("@Holiday", DbType.String),
                    new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = model.StaffID;
            parameters[1].Value = model.HolidayDate;
            parameters[2].Value = model.ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Holiday ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Holiday ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Accounting.Model.Holiday GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,Holiday from Holiday ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            Accounting.Model.Holiday model = new Accounting.Model.Holiday();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StaffID"] != null && ds.Tables[0].Rows[0]["StaffID"].ToString() != "")
                {
                    model.StaffID = int.Parse(ds.Tables[0].Rows[0]["StaffID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Holiday"] != null && ds.Tables[0].Rows[0]["Holiday"].ToString() != "")
                {
                    model.HolidayDate = ds.Tables[0].Rows[0]["Holiday"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,Holiday ");
            strSql.Append(" FROM Holiday ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "Holiday";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion Method

        internal void AddBatch(System.Collections.Generic.List<Model.Holiday> lstHoliday, List<string[]> calcMonth)
        {
            StringBuilder strSql = new StringBuilder();
            ArrayList listSql = new ArrayList();
            foreach (string[] kk in calcMonth)
            {
                strSql.Append("delete from Holiday ");
                strSql.Append(" Where StaffID= " + kk[0]);
                strSql.Append(" And substr(Holiday, 0, 7) = '" + kk[1] + "'");

                listSql.Add(strSql.ToString());
            }
            foreach (Model.Holiday kk in lstHoliday)
            {
                strSql = new StringBuilder();
                strSql.Append("insert into Holiday(");
                strSql.Append("StaffID,Holiday)");
                strSql.Append(" values (");
                strSql.Append(kk.StaffID);
                strSql.Append(",'");
                strSql.Append(kk.HolidayDate).Append("')");
                strSql.Append(";select LAST_INSERT_ROWID()");

                listSql.Add(strSql.ToString());
            }
            DbHelperSQLite.ExecuteSqlTran(listSql);
        }

        internal int GetCount(string p, string staffID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(ID) as CountID ");
            strSql.Append(" FROM Holiday ");
            strSql.Append(" Where StaffID= " + staffID);
            strSql.Append(" And substr(Holiday, 0, 7) = '" + p + "'");

            DataSet ds = DbHelperSQLite.Query(strSql.ToString());

            try
            {
                return int.Parse(ds.Tables[0].Rows[0]["CountID"].ToString());
            }
            catch
            {
                return 0;
            }
        }
    }
}